<template>
  <el-container class="header_box">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          router
          :default-active="activePath"
        >
          <!-- 一级导航列表 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsub[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级导航列表 -->
            <el-menu-item
              :index="'/'+ subItem.path"
              v-for="subItem in item.children"
              :key="subItem"
              @click="saveNav('/'+ subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [],
      iconsub: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activePath: ''
    }
  },
  // 生命周期函数
  created() {
    this.getMenu(),
      (this.activePath = window.sessionStorage.getItem('activePath'))
  },
  methods: {
    logout() {
      //点击退出按钮
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //获取侧边栏数据
    async getMenu() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    saveNav(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.header_box {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    div {
      display: flex;
      align-items: center;
      img {
        margin-right: 10px;
      }
      span {
        font-size: 20px;
      }
    }
  }
  .el-container {
    .el-aside {
      background-color: #333744;
      .el-menu {
        border-right: none;
      }
    }
    .el-main {
      background-color: #eaedf1;
    }
  }
}
.iconfont {
  margin-right: 10px;
}
</style>
